package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Component
public class SpringRabbitListener {

    /**
     * 最简单的监听消息
     * @param message
     */
    @RabbitListener(queues = "heima.queue1")
    public void listenSimpleQueue(Map<String,Object> message){
        System.out.println("消费者接收到simple.queue的消息:["+message+"]");
    }

    /**
     * 同时接收消息 共50条
     * @param message 接收到的消息
     * @throws InterruptedException
     */
//    @RabbitListener(queues = "heima.queue1")
//    public void listenWorkQueue1(String message) throws InterruptedException {
//        System.out.println("消费者1接收到消息:["+message+"]"+ LocalTime.now());
//        Thread.sleep(20);
//    }

    /**
     * 同时接收消息 共50条
     * @param message 接收的消息
     * @throws InterruptedException
     */
//    @RabbitListener(queues = "heima.queue1")
//    public void listenWorkQueue2(String message) throws InterruptedException {
//        System.err.println("消费者2...接收到消息:["+message+"]"+LocalTime.now());
//        Thread.sleep(200);
//    }

    /**
     * 自定义一个Fanout交换机 同时绑定两个队列
     * @param message
     * @throws InterruptedException
     */
//    @RabbitListener(queues = "fanout.queue1")
//    public void listenWorkQueue3(String message) throws InterruptedException {
//        System.err.println("消费者1...接收到消息:["+message+"]"+LocalTime.now());
//    }

    /**
     * 自定义一个Fanout交换机 同时绑定两个队列
     * @param message
     * @throws InterruptedException
     */
//    @RabbitListener(queues = "fanout.queue2")
//    public void listenWorkQueue4(String message) throws InterruptedException {
//        System.err.println("消费者2...接收到消息:["+message+"]"+LocalTime.now());
//    }


    /**
     * 基于注解声明队列和交换机
     * @param msg
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue1"),
//            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
//            key = {"red","blue"}
//    ))
//    public void listenDirectQueue1(String msg){
//        System.out.println("消费者接收到direct.queue1的消息: ["+msg+"]");
//    }

    /**
     * 基于注解声明队列和交换机
     * @param msg
     */
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue2"),
//            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
//            key = {"red","yellow"}
//    ))
//    public void listenDirectQueue2(String msg){
//        System.out.println("消费者接收到direct.queue2的消息: ["+msg+"]");
//    }


    /**
     * topic 声明ROuting key 满足条件则监听消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者接收到topic,queue1的消息: "+msg);
    }

    /**
     * topic 声明ROuting key 满足条件则监听消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.err.println("消费者接收到topic,queue2的消息: "+msg);
    }




}
